#include "SpatialPartitioningFactory.h"

#include "../include/spatial_partitioning/UniformGridConfig.h"
#include "../include/spatial_partitioning/NullSpatialPartitioningConfig.h"
#include "UniformGrid.h"
#include "NullSpatialPartitioning.h"
#include "pw_exception.h"

namespace physicsworks {

using exception::IBadArg;

SpatialPartitioning* SpatialPartitioningFactory::instance(SpatialPartitioningType s, const SpatialPartitioningConfig& sc,
                                                          const vec3& a, const vec3& b)
{
	const UniformGridConfig* ugc;
	const NullSpatialPartitioningConfig* nc;
	
	switch (s) {
	case UNIFORM_GRID:
		ugc = dynamic_cast<const UniformGridConfig*>(&sc);
		Assert<IBadArg>(ugc != 0, "The SpatialPartitioningConfig must be of the type UniformGridConfig in this case");
		return new UniformGrid(a, b, ugc->cellDiameter, ugc->numBuckets);
	case NULL_SPATIAL_PARTITIONING:
		nc = dynamic_cast<const NullSpatialPartitioningConfig*>(&sc);
		Assert<IBadArg>(nc != 0, "The SpatialPartitioningConfig must be of the type NullSpatialPartitioningConfig in this case");
		return new NullSpatialPartitioning(a, b);
	}
	
	throw IBadArg("Unrecognizable spatial partitioning type");
}

}
